<template>
  <a-modal
    :open="open"
    width="640px"
    title="新增质控检测"
    :body-style="{ height: '280px', overflow: 'auto' }"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-radio-group v-model:value="tabActiveKey" button-style="solid" @change="getData">
      <a-radio-button :value="1">完整性质控</a-radio-button>
      <a-radio-button :value="2" disabled>准确性质控</a-radio-button>
      <a-radio-button :value="3" disabled>规范性质控</a-radio-button>
      <a-radio-button :value="4" disabled>逻辑性质控</a-radio-button>
    </a-radio-group>
    <a-form :model="formState" class="form-style-flex mt-5">
      <template v-if="tabActiveKey === 1 || tabActiveKey === 2">
        <a-form-item label="项目名称" class="w-full" v-bind="validateInfos.itemId">
          <SelectTable
            v-model:value="formState.itemName"
            type="质控规则"
            @select="
              (select) => {
                formState.itemName = select?.itemName
                formState.itemId = select?.itemId
              }
            "
          />
        </a-form-item>
      </template>
      <template v-if="tabActiveKey === 1">
        <a-form-item label="是否必填" class="w-full">
          <a-switch v-model:checked="formState.isRequired" />
        </a-form-item>
        <a-form-item label="备注" class="w-full">
          <a-textarea v-model:value="formState.memo" placeholder="请输入" :rows="4" />
        </a-form-item>
      </template>
      <template v-if="tabActiveKey === 2">
        <a-form-item label="标准模版" class="w-full">
          <a-input v-model:value="formState.bzmb" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="最小值" class="w-1/2">
          <a-input v-model:value="formState.zxz" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="最大值" class="w-1/2">
          <a-input v-model:value="formState.zdz" placeholder="请输入" />
        </a-form-item>
      </template>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
// import SelectItem from '@/components/SelectItem'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:open', 'success'])
const tabActiveKey = ref(1)
const formState = ref({
  itemId: ''
})
const rulesState = ref({
  itemId: [{ required: true, message: '请选择项目' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/med/MedicalRecord/doSaveMrQcrule', {
    ...formState.value,
    qcruleType: tabActiveKey.value,
    isRequired: formState.value.isRequired ? 1 : 0
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  resetFields()
  emit('update:open', false)
  emit('success')
}
const onCancel = () => {
  resetFields()
  emit('update:open', false)
}

watch(
  () => props.open,
  () => {
    if (props.rowData?.id) {
      formState.value = { ...props.rowData, isRequired: props.rowData.isRequired === 1 ? true : false, status: props.rowData.status ? 1 : 0 }
    } else {
      formState.value = { isRequired: true }
    }
  }
)
</script>
<style lang="less" scoped></style>
